package Test.StirngTest;

import java.util.*;
import java.util.stream.Collectors;

class Student {
    private String name;
    private double score;
    private double height;

    public Student(String name, double score, double height) {
        this.name = name;
        this.score = score;
        this.height = height;
       }

    public void setName(String name) {
        this.name = name;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", height=" + height +
                '}';
    }

    public String getName() {
        return name;
    }

    public double getScore() {
        return score;
    }

    public double getHeight() {
        return height;
    }


}

public class StudentSortingExample {
    public static void main(String[] args) {
        // 创建学生对象列表
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 85.5, 165.0));
        students.add(new Student("Bob", 90.0, 170.0));
        students.add(new Student("Charlie", 80.0, 175.0));
        students.add(new Student("David", 90.0, 160.0));

        Map<Double, Double> collect = students.stream().collect(Collectors.groupingBy(
                Student::getScore, Collectors.summingDouble(Student::getHeight)
        ));
        System.out.println(collect);

        Map<Double, Student> scoreToStudentMap = students.stream()
                .collect(Collectors.toMap(Student::getScore,              // key: 成绩
                        student -> student,                              // value: 学生对象
                        (existingStudent, newStudent) -> {               // 合并函数: 将身高累加
                            existingStudent.setHeight(existingStudent.getHeight() + newStudent.getHeight());
                            return existingStudent;
                        }));

//        System.out.println(scoreToStudentMap);
        for (Map.Entry<Double,Student> m: scoreToStudentMap.entrySet()
             ) {
            System.out.println(m.getValue().toString());
        }

        // 按照成绩排序
//        匿名内部类可以用来实现接口或抽象类，并重写其中的方法。
//        在这里，我们实现了Comparator<Student>接口，并重写了compare方法来定义学生对象的比较逻辑。
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // 先比较成绩
                int scoreComparison = Double.compare(s2.getScore(), s1.getScore());
                if (scoreComparison != 0) {
                    return scoreComparison;
                }
                // 如果成绩相同，则比较身高
                return Double.compare(s2.getHeight(), s1.getHeight());
            }
        });
/*        Collections.sort(students, (s1, s2) -> {
            // 先比较成绩
            int scoreComparison = Double.compare(s2.getScore(), s1.getScore());
            if (scoreComparison != 0) {
                return scoreComparison;
            }
            // 如果成绩相同，则比较身高
            return Double.compare(s2.getHeight(), s1.getHeight());
        });*/
        // 使用Comparator.comparing()和方法引用进行排序
/*        students.sort(
                Comparator.comparing(Student::getScore)
                        .thenComparing(Student::getHeight).reversed()
        );*/
/*        // 使用流API进行排序
        List<Student> sortedStudents = students.stream()
                .sorted((s1, s2) -> {
                    // 先比较成绩
                    int scoreComparison = Double.compare(s2.getScore(), s1.getScore());
                    if (scoreComparison != 0) {
                        return scoreComparison;
                    }
                    // 如果成绩相同，则比较身高
                    return Double.compare(s2.getHeight(), s1.getHeight());
                })
                .collect(Collectors.toList());*/

        // 打印排序结果
        for (Student student : students) {
            System.out.println("Name: " + student.getName() + ", Score: " + student.getScore() + ", Height: " + student.getHeight());
        }
    }
}